package com.yx.system.controller;


import com.baomidou.mybatisplus.plugins.Page;
import com.yx.common.annotation.Log;
import com.yx.common.annotation.LoginUser2;
import com.yx.common.utils.Query;
import com.yx.common.utils.R;
import com.yx.common.validator.Assert;
import com.yx.common.validator.ValidatorUtils;
import com.yx.common.validator.group.UpdateGroup;
import com.yx.modules.api.entity.TbUser;
import com.yx.modules.api.service.TbTokenService;
import com.yx.modules.api.service.TbUserService;
import com.yx.system.entity.SysUser;
import com.yx.system.entity.SysUserRole;
import com.yx.system.service.SysUserRoleService;
import com.yx.system.service.SysUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 后台会员管理控制器
 */
@RestController
@RequestMapping("/sys/tbuser")
public class TbUserController {

    @Autowired
    private TbUserService tbUserService;

    @Autowired
    private TbTokenService tbTokenService;

    @Autowired
    private SysUserRoleService sysUserRoleService;

    @Autowired
    private SysUserService sysUserService;


    /**
     * 所有用户列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("sys:tbuser:list")
    public R list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);
        Page<TbUser> pageUtil = new Page<TbUser>(query.getPage(), query.getLimit());
        Page<TbUser> page = tbUserService.queryPageList(pageUtil, query);
        return R.ok().put("page", page);
    }

    /**
     * 按UID查询用户
     *
     * @return
     */
    @PostMapping("findByUid")
    @RequiresPermissions("sys:tbuser:findByUid")
    public R findByUId(String uid) {
        return R.ok().put("user", tbUserService.queryByUid(uid));
    }

    /**
     * 根据ID查询用户
     * @param userId
     * @return
     */
    @RequestMapping("/findById/{userId}")
    @RequiresPermissions("sys:tbuser:findById")
    public R findById(@PathVariable("userId") Long userId){
        TbUser tbUser = tbUserService.foundById(userId);
        return R.ok().put("tbuser",tbUserService.foundById(userId));

    }

//    /*
//    * 成为作者
//    */
//    @Log("成为作者")
//    @RequestMapping("/beauthor")
//    @RequiresPermissions("sys:tbuser:beauthor")
//    public R beAuthor(@RequestBody TbUser tbUser1, @LoginUser2 SysUser sysUser1){
//        TbUser tbUser = tbUserService.foundById(Long.valueOf(tbUser1.getUserId()));
//        if (tbUser.getIdentity()==1){
//            return R.error("用户已经是作者了");
//        }
//        SysUser sysUser = new SysUser();
//        sysUser.setCreateUserId(sysUser1.getUserId());
//        sysUser.setCreateTime(new Date());
//        return sysUserService.beAuthor(tbUser,sysUser);
//    }


    /**
     * 修改用户
     */
    @Log("修改用户")
    @RequestMapping("/update2")
    @RequiresPermissions("sys:tbuser:update")
    public R updates(@RequestBody TbUser tbuser){
        Assert.isBlank(tbuser.getUsername(),"会员名不能为空");
        Assert.isBlank(tbuser.getEmail(),"邮箱不能为空");
        Assert.isBlank(tbuser.getMobile(),"手机号不能为空");
        ValidatorUtils.validateEntity(tbuser, UpdateGroup.class);
        TbUser tb = tbUserService.foundById(Long.valueOf(tbuser.getUserId()));
        tbuser.setPassword(tb.getPassword());
        tbUserService.update(tbuser);

        return R.ok();
    }

    /**
     * 更新
     *
     * @return
     */
    @PostMapping("update")
    @RequiresPermissions("sys:tbuser:update")
    public R update(TbUser tbUser) {
        return R.ok().put("user", tbUserService.updateById(tbUser));
    }
}
